Description: 最近一个项目可能要用到socket和串口方面的编程,网上找了很多这方面的资料,例子,不过都不怎么全,而且很多例子不能直接拿来用。花了几天时间,将TCP、UDP、串口整合到一起做了个小的project,由于没有真机,仅在模拟器上测试通过,且都是在一台机子上,现贴出代码,希望对大家有所帮助。注意涉及IP地址的地方可能需要修改成你自己的地址-A recent project you might want to use the socket and serial programming, online to find a lot of information in this regard, for example, but not all, examples and many cannot directly use them. It took a couple of days, TCP, and UDP, serial ports, combined together to do a small project, since there is no real machine, only on the simulator tests pass, and is on a single machine, post code, hoped has the help to everybody. Note relating to local IP address may need to be modified to your own address Platform: |
Size: 15360 |
Author:李永芳 |
Hits:
Description: a.确定本机的IP和端口,socket只有与某一IP和端口绑定,才能发挥其功能。
b.确定通讯协议:TCP 或 UDP。
编程步骤:
a.建立一个套接字
b.绑定本机的IP和端口
c.如果是TCP,因为是面向连接的,要利用Listen()方法来监听网络上是否有人给自己发东西 如果是UDP,因为是无连接的,所以来者不拒。
d.TCP情况下,如果监听到一个连接,就可以使用accept来接收这个连接,然后就可以利用Send/Receive来执行操作了。而UDP,则不需要accept,直接使用SendTo/ReceiveFrom来执行操作。(注意,因为UDP不需要建立连接,所以在发送前并不知道对方的IP和端口,因此需要指定一个发送的节点才能进行正常的发送和接收)
e.不继续发送和接收时使用close。-a. determine the machine s IP and port, socket only with an IP and port binding, in order to function.
. b determine protocols: TCP or UDP.
Programming steps:
a. to establish a socket
b. Bind the machine IP and port
. c If TCP, because it is connection-oriented, and to take advantage of Listen () method to listen if anyone on the network to send their own thing if it is UDP, because it is not connected, so all comers.
Under d.TCP case, if listening to a connection, you can use the accept to accept the connection, then you can use the Send/Receive to perform the operation. The UDP, you do not need accept, directly SendTo/ReceiveFrom to perform the operation. (Note that because UDP does not need to establish a connection, so before sending did not know each other s IP and port, so you need to specify a sending node can perform normal transmission and reception)
e. not to continue sending and receiving using close. Platform: |
Size: 3072 |
Author:刘洋 |
Hits:
Description: VC++局域网聊天程序,利用Socket套接字将主程序分为服务端和客户端两个部分。采用了UDP和TCP/IP相结合,有效提高了数据传输的时效性和稳定性。本程序还用到了多线程编程,用来解决程序自身阻塞的问题。(The VC ++ LAN chat program uses Socket to divide the main program into two parts: server and client. The combination of UDP and TCP / IP is adopted to effectively improve the timeliness and stability of data transmission. This program also uses multi-threaded programming to solve the problem of program blocking itself.) Platform: |
Size: 234496 |
Author:wzp342 |
Hits: